﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class Spawner : MonoBehaviour {

	private Pool<GameObject> Red;	//Red Checkers
	private Pool<GameObject> White; //White Checkers
	public Vector3 BoardPosition; //Current board coords
	public Transform PoolDest;
	public GameObject RedPrefab;
	public GameObject WhitePrefab;
   
    

	const float BoardCf=0.1f;



	public class Pool<T>{
		List<T> objects=new List<T>();
		T basicobj; //For adding

		public Pool(T obj){
			basicobj=obj; 
		}

		public void Add(T obj){
			objects.Add(obj);
		}

		public T Get(){
			if(objects.Count==0)return default(T);
			else{
				T obj=objects[0];
				objects.RemoveAt(0);
				return obj;
			}
		}

		public void Clear(){
			objects.Clear();
		}

	}


	void Start () {
		Red = new Pool<GameObject>(RedPrefab);
		White = new Pool<GameObject>(WhitePrefab);
		FillPool(Red, RedPrefab, 8); 	//Fill Red
		FillPool(White, WhitePrefab, 8);	//Fill White

		SpawnLine (0,4); //Spawn Red Line
		SpawnLine (0,-3); // Spawn White Line
	}
	

	void Update () {
		if (Input.GetMouseButtonDown(0)){  //Spawn under mouse
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast(ray, out hit)){
					GameObject go;
					go=Red.Get ();
					if(go!=null){
						go.transform.position=hit.point;
						go.SetActive(true);
					}
					else{
						go=(GameObject)Instantiate(
									RedPrefab,
					                hit.point,
					                Quaternion.identity);
					}
			}
		}

		if (Input.GetMouseButtonDown(1)){ 	//Send to pool
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast(ray, out hit)){
				if(hit.collider.name=="White(Clone)" || hit.collider.name=="Black(Clone)"){
					Red.Add(hit.collider.gameObject);
					hit.collider.gameObject.transform.position=PoolDest.position;
					hit.collider.gameObject.SetActive(false);
				}
			}
		}
	}

	public void FillPool(Pool<GameObject> pool, GameObject go, int Size){	//Fill with GameObjects
		for(int i=0; i<Size; i++){
			GameObject obj;
			obj=(GameObject)Instantiate(go, PoolDest.position, Quaternion.identity);
			obj.SetActive(false);
			pool.Add(obj);
		}
	}

	public void SpawnVector3(int col, Vector3 pos){//Spawn Checker by Coords
		if (col == 0) {
			GameObject go;
			go=Red.Get();
			if(go!=null){
				go.transform.position=pos;
				go.SetActive(true);
			}
			else{
				go=(GameObject)Instantiate(
					RedPrefab,
					pos,
					Quaternion.identity);
			}
		}else{
			GameObject go;
			go=White.Get();
			if(go!=null){
				go.transform.position=pos;
				go.SetActive(true);
			}
			else{
				go=(GameObject)Instantiate(
					RedPrefab,
					pos,
					Quaternion.identity);
			}
		}
	}

	public void SpawnXY(int col ,int x, int y){//Spawn Checker
				SpawnVector3 (col, new Vector3 (
					BoardPosition.x + BoardCf + BoardCf * 2 * (x - 1),
				    BoardPosition.y + 0.04f,
				    BoardPosition.z + BoardCf + BoardCf * 2 * (y - 1)));

	}

	public void SpawnLine(int col, int line){//Spawn Line
		if (col == 0){
			for(int i=-3; i<5; i++){
				SpawnXY(col, line, i);
			}
		}else{
			for(int i=-3; i<5; i++){
				SpawnXY(col, line, i);
			}
		}
	}

	public void Spawn1Button(){
		SpawnLine(0,2);
	}
	


}
